//
//  SVProgressHUDTool.m
//  PipiWallpaper
//
//  Created by Yan on 2020/7/2.
//  Copyright © 2020 PipiWallpaper. All rights reserved.
//

#import "SVProgressHUDTool.h"
#import "SVProgressHUD.h"
#import "UIView+Toast.h"

@implementation SVProgressHUDTool
+ (void)showWithStatus:(nullable NSString *)status{
//    [DVLoadingView showWithTip:status];
    [self showWithStatus:status dismissTimeInterval:0 completion:nil];
}
+ (void)showWithStatus:(NSString *)status dismissTimeInterval:(NSInteger)interval completion:(nullable SVProgressHUDShowCompletion)completion{
    [self initDefaultStyleCompletion:completion dismissTimeInterval:interval];
    [SVProgressHUD showWithStatus:status];
}

+ (void)showSuccessWithStatus:(NSString *)status{
    [self showSuccessWithStatus:status completion:^{}];
}
+ (void)showSuccessWithStatus:(NSString *)status dismissTimeInterval:(NSInteger)interval completion:(SVProgressHUDShowCompletion)completion{
    [self initDefaultStyleCompletion:completion dismissTimeInterval:interval];
    [SVProgressHUD showSuccessWithStatus:status];
}
+ (void)showSuccessWithStatus:(NSString *)status completion:(SVProgressHUDShowCompletion)completion{
    [self initDefaultStyleCompletion:completion dismissTimeInterval:1.3];
    [SVProgressHUD showSuccessWithStatus:status];
}

+ (void)showInfoWithStatus:(NSString *)status{
    [self showInfoWithStatus:status completion:^{}];
}

+ (void)showInfoWithStatus:(NSString *)status completion:(SVProgressHUDShowCompletion)completion{
    [self initDefaultStyleCompletion:completion dismissTimeInterval:1.3];
    [SVProgressHUD showInfoWithStatus:status];
}


+ (void)showErrorWithStatus:(NSString *)status{
//    [self showErrorWithStatus:status completion:^{}];
    dispatch_async(dispatch_get_main_queue(), ^{
        // 需要在主线程执行的代码
        [self showToastMessage:status];
    });
    
}
+ (void)showErrorWithStatus:(NSString *)status completion:(SVProgressHUDShowCompletion)completion{
    [self showErrorWithStatus:status dismissTimeInterval:1 completion:completion];
}
+ (void)showErrorWithStatus:(NSString *)status dismissTimeInterval:(NSInteger)interval completion:(SVProgressHUDShowCompletion)completion{
    [self initDefaultStyleCompletion:completion dismissTimeInterval:interval];
    [SVProgressHUD showErrorWithStatus:status];
}

+ (void)showProgress:(CGFloat)progress status:(NSString *)status{
    [self initDefaultStyleCompletion:nil dismissTimeInterval:1];
    [SVProgressHUD showProgress:progress status:status];
}

+ (void)showToastMessage:(NSString *)message{
    [self showToastMessage:message completion:^{}];
}
+ (void)showToastTitle:(NSString *)title Message:(NSString *)message{
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUDTool dismiss];
        
        UIWindow *window = [[UIApplication sharedApplication] delegate].window;
//        [CSToastManager sharedStyle].backgroundColor = COLOR(20, 24, 31);//[[UIColor blackColor] colorWithAlphaComponent:0.5];
//        [CSToastManager setDefaultDuration:1];
//        [[CSToastManager sharedStyle] setShadowRadius:16];
//        [CSToastManager sharedStyle].messageFont = [UIFont systemFontOfSize:12];
//        [CSToastManager sharedStyle].messageAlignment = NSTextAlignmentCenter;
//        [CSToastManager sharedStyle].messageColor = TFCOLOR_ALPH(255, 255, 255,0.55);
//        [CSToastManager sharedStyle].titleFont = [UIFont systemFontOfSize:14];
//        [CSToastManager sharedStyle].titleColor = COLOR(255, 255, 255);
//        [CSToastManager sharedStyle].titleAlignment = NSTextAlignmentCenter;
//        [CSToastManager sharedStyle].horizontalPadding = 24;
//        [CSToastManager sharedStyle].verticalPadding = 14;
//        [window makeToast:message duration:[CSToastManager defaultDuration] position:[CSToastManager defaultPosition] title:title image:nil style:nil completion:^(BOOL didTap) {
//            
//        }];
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)([CSToastManager defaultDuration] * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            if(completion){
//                completion();
//            }
//        });
    });
}

+ (void)showToastMessage:(NSString *)message completion:(SVProgressHUDShowCompletion)completion{
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUDTool dismiss];
        
//        UIWindow *window =  [TFAppInfo share].commentDarkModel ? [UIApplication sharedApplication].keyWindow :  [[UIApplication sharedApplication] delegate].window;
//        [CSToastManager sharedStyle].backgroundColor = COLOR(20, 24, 31);//[[UIColor blackColor] colorWithAlphaComponent:0.5];
//        [CSToastManager setDefaultDuration:1];
//        [[CSToastManager sharedStyle] setShadowRadius:16];
//        [CSToastManager sharedStyle].messageFont = [UIFont systemFontOfSize:14];
//        [CSToastManager sharedStyle].messageColor = COLOR(255, 255, 255);
//        [CSToastManager sharedStyle].horizontalPadding = 24;
//        [CSToastManager sharedStyle].verticalPadding = 14;
//        [window makeToast:message];
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)([CSToastManager defaultDuration] * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            if(completion){
//                completion();
//            }
//        });
    });
}

+ (void)initDefaultStyleCompletion:(SVProgressHUDShowCompletion)completion dismissTimeInterval:(NSInteger)interval{
//    [SVProgressHUD setBorderColor:[UIColor clearColor]];
//    [SVProgressHUD setForegroundColor:[UIColor clearColor]];
//    [SVProgressHUD setForegroundImageColor:[UIColor clearColor]];
//    [SVProgressHUD setBackgroundColor:[UIColor clearColor]];
//    [SVProgressHUD setBackgroundLayerColor:[UIColor clearColor]];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];//SVProgressHUDStyleLight];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeCustom];
    [SVProgressHUD setCornerRadius:16];
    [SVProgressHUD setFont:[UIFont systemFontOfSize:12.f weight:UIFontWeightMedium]];
    
    if(interval){
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(interval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if(completion){
                [SVProgressHUD dismiss];
                completion();
            }
        });
    }
}

+ (void)dismiss{
    [SVProgressHUD dismiss];
//    [DVLoadingView hide];
}
@end
